package com.orange.observerPatterns;

import java.util.List;

public class MySubjectImpl implements MyObserverable{
    //维护一个观察者列表，当对象状态发生变化时，通知所有观察者
    private List<MyObserver> observerList;

    //初始化列表，使用构造器
    public MySubjectImpl(List<MyObserver> list){
        this.observerList=list;
    }

    @Override
    public void registerObserver(MyObserver o) {
        observerList.add(o);
    }

    @Override
    public void removeObserver(MyObserver o) {
        observerList.remove(o);
    }

    @Override
    public void notifyObservers() {
        for(MyObserver observer:observerList){
            observer.update("mySubject has changed");
        }
    }
}
